<?php 
	namespace Models;

	class Text
	{	
		private $app;
		private $access = 3;
		function __construct($app){
			$this->app = $app;
		}

		function getAll(){
			/*$sql = "SELECT idseries series_id, series_type, series_name, series_season, series_count, series_link, series_date FROM series";
			return json_encode($this->app['db']->fetchAll($sql));*/
		}

		function getOne($name){

			$sql = "SELECT text_pages_text text, text_pages_title title, text_pages_description description, text_pages_project project  FROM text_pages WHERE text_pages_name = ?";
			return $this->app['db']->fetchAssoc($sql, array( (string) $name ));	
		}

		function insert($data){
			$arr['text_pages_title'] =  htmlspecialchars($data['text_pages_title'] );
			$arr['text_pages_text'] = htmlspecialchars($data['text_pages_text']);
			$arr['text_pages_project'] = $data['text_pages_project'];
			$arr['text_pages_description'] = htmlspecialchars($data['text_pages_description']);

			$arr['text_pages_name']  = $data['text_pages_name'];
			$this->app['db']->insert('text_pages', $arr);
			return '';
		}

		function update($data){

			$arr['text_pages_title'] =  $data['title'] ;
			$arr['text_pages_text'] = $data['text'];
			$arr['text_pages_description'] = $data['description'];
			$arr['text_pages_project'] = $data['project'];
			
			$name  = $data['name'];
			$arr['text_pages_name'] = $name;
		
			$ex = $this->getOne($name);
			if(!$ex){
				$this->insert($arr);
			}else{
				foreach ($arr as $key => $value) {
					$arr[$key] = htmlspecialchars($value);
				}
				$arr1['text_pages_title'] =$arr['text_pages_title'];
				$arr1['text_pages_text'] =$arr['text_pages_text'];
				$arr1['text_pages_description'] =$arr['text_pages_description'];
				$arr1['text_pages_name'] = $name;

				$this->app['db']->update('text_pages', $arr1, array( 'text_pages_name'=> $name ));
			}

		}

		function delete($data){
			if( isset($data['series_id']) ){
				$this->app['db']->delete('series', array('idseries'=>$data['series_id'] ));
			}
		}

	}

?>